package com.kkb.service;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.kkb.mapper.MenuMapper;
import com.kkb.pojo.Menu;
import com.kkb.pojo.MenuExample;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;

@Service
public class MenuService {
    @Resource
    private MenuMapper menuMapper;

    @Transactional(propagation = Propagation.REQUIRED, readOnly = true)
    public PageInfo<Menu> queryByPage(Integer pageNum, Integer pageSize, String menuName) {
        MenuExample menuExample = new MenuExample();
        MenuExample.Criteria criteria = menuExample.createCriteria();
        if (menuName != null && menuName.trim().length() > 0) {
            criteria.andMenuNameLike("%" + menuName.trim() + "%");
        }
        criteria.andIsDelEqualTo(0);
        PageHelper.startPage(pageNum, pageSize);
        List<Menu> menuList = menuMapper.selectByExample(menuExample);
        return new PageInfo<>(menuList,5);
    }

    /**
     * 添加角色动态获取菜单列表，获取的是没有被删除的，和状态是处于启用的菜单列表
     * @return
     */
    @Transactional(propagation = Propagation.REQUIRED, readOnly = true)
    public List<Menu> queryMenuList() {
        MenuExample menuExample = new MenuExample();
        MenuExample.Criteria criteria = menuExample.createCriteria();
        criteria.andIsDelEqualTo(0);
        criteria.andMenuStateEqualTo(0);
        List<Menu> menuList = menuMapper.selectByExample(menuExample);
        return menuList;
    }

    @Transactional(propagation = Propagation.REQUIRED, readOnly = true)
    public Menu queryById(Integer menuId) {
        return menuMapper.selectByPrimaryKey(menuId);
    }
    @Transactional(propagation = Propagation.REQUIRED, readOnly = true)
    public List<Menu> queryByMenuName(String menuName) {
        MenuExample menuExample = new MenuExample();
        MenuExample.Criteria criteria = menuExample.createCriteria();
        criteria.andMenuNameEqualTo(menuName);
        //criteria.andIsDelEqualTo(0);
        List<Menu> menuList = menuMapper.selectByExample(menuExample);
        return menuList;
    }
    @Transactional(propagation = Propagation.REQUIRED, rollbackFor = {Exception.class})
    public int addMenu(Menu menu) {
        menu.setIsDel(0);
        return menuMapper.insertSelective(menu);
    }


    @Transactional(propagation = Propagation.REQUIRED, rollbackFor = {Exception.class})
    public int deleteMenu(Integer menuId) {
        Menu menu = new Menu();
        menu.setMenuId(menuId);
        menu.setIsDel(1);
        return menuMapper.updateByPrimaryKeySelective(menu);
    }

    @Transactional(propagation = Propagation.REQUIRED, rollbackFor = {Exception.class})
    public int updateMenu(Menu menu) {
        MenuExample menuExample = new MenuExample();
        MenuExample.Criteria criteria = menuExample.createCriteria();
        criteria.andMenuNameEqualTo(menu.getMenuName());
        criteria.andMenuIdNotEqualTo(menu.getMenuId());
       // criteria.andIsDelEqualTo(0);
        List<Menu> menuList = menuMapper.selectByExample(menuExample);
        if(menuList.size()>0){
            return -1;
        }
        return menuMapper.updateByPrimaryKeySelective(menu);
    }

    @Transactional(propagation = Propagation.REQUIRED, rollbackFor = {Exception.class})
    public int deleteMenus(List<Integer> menuIds) {
        int i = menuMapper.deleteMenus(menuIds);
        return i;
    }
}

